package com.plant.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.plant.entity.UgcUser;
import com.plant.service.UserServic;
import com.plant.tool.HeadPhotoImageZipUtil;

@Controller
@RequestMapping(value = "/user1_1_2")
public class UpadteUserInfContraller {
	@Resource(name = "userInfoImlp")
	private UserServic userServic;

	@RequestMapping(value = "update_headphoto")//1用户修改用户头像
	@ResponseBody
	public  Map<String, Object> UpdateHeadphoto(@RequestParam(value = "file", required = false) MultipartFile myfile,
			Integer user_id,UgcUser u,HttpServletResponse response,String secret,
			String pust_val,HttpServletRequest request) throws IOException {
		response.setHeader("access-control-allow-origin","*");
		Map<String,Object> map=new HashMap<String, Object>();
		if(pust_val==null||secret==null||user_id==null||myfile==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
			String mds=Md.MdSecret(pust_val+"flower_duigu2016");
			if(secret.endsWith(mds)){
				u.setId(user_id);
				List<Object> userinf=new ArrayList<Object>();
				userinf=userServic.getByuid(u);
				String photoName=null;
				String fileName=myfile.getOriginalFilename();
				fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
				String extName = fileName.substring(fileName.lastIndexOf("."));//.jpg
				String uuid = UUID.randomUUID().toString().replace("-", "");
				String newName = uuid+extName;   
				String realPath = request.getSession().getServletContext().getInitParameter("UPLOAD_IMAGE_PATH");//从配置文件中读取要保存的位置 
				org.apache.commons.io.FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath,newName));  
				photoName="http://www.zhiwuxiansheng.com:8080/images/HeadPhoto/"+newName;
				HeadPhotoImageZipUtil img=new HeadPhotoImageZipUtil();
				img.zipImageFile(new File(realPath,newName), new File(realPath,newName), 640, 640, 0.6f);
				for (Object object : userinf) {
					UgcUser user=(UgcUser) object;
					user.setPhoto(photoName);
					boolean b=userServic.updateU(user);
					if(b){
						map.put("message", "修改头像成功");
						map.put("state", 1);
					}else{
						map.put("message", "修改头像失败");
						map.put("state", 0);
					}
				}
				return map;
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 1);
				return map;
			}
		}
	}


	@RequestMapping(value = "update_backgroundimage")//用户修改背景图片
	@ResponseBody
	public  Map<String, Object> UpdateBackgroundimage(@RequestParam(value = "file", required = false) MultipartFile[] myfiles,
			Integer user_id,UgcUser u,HttpServletResponse response,String secret,String pust_val,
			HttpServletRequest request) throws IOException {
		response.setHeader("access-control-allow-origin","*");
		Map<String,Object> map=new HashMap<String, Object>();
		if(pust_val==null||secret==null||user_id==null||myfiles==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
			String mds=Md.MdSecret(pust_val+"flower_duigu2016");
			if(secret.endsWith(mds)){
				u.setId(user_id);
				List<Object> userinf=new ArrayList<Object>();
				userinf=userServic.getByuid(u);
				String photoName=null;
				for(MultipartFile myfile : myfiles){  
					if(myfile.isEmpty()){  
						return null;
					}else{  
						String fileName=myfile.getOriginalFilename();
						fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
						String extName = fileName.substring(fileName.lastIndexOf("."));//.jpg
						String uuid = UUID.randomUUID().toString().replace("-", "");
						String newName = uuid+extName;   
						String realPath = request.getSession().getServletContext().getInitParameter("UPLOAD_IMAGE_PATH");//从配置文件中读取要保存的位置 
						org.apache.commons.io.FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath,newName));  
						photoName="http://www.zhiwuxiansheng.com:8080/images/HeadPhoto/"+newName;
						HeadPhotoImageZipUtil img=new HeadPhotoImageZipUtil();
						img.zipImageFile(new File(realPath,newName), new File(realPath,newName), 640, 640, 0.6f);
					}  
				}  
				for (Object object : userinf) {
					UgcUser user=(UgcUser) object;
					user.setBackgroundImage(photoName);
					boolean b=userServic.updateU(user);
					if(b){
						map.put("message", "修改背景图成功");
						map.put("state", 1);
					}else{
						map.put("message", "修改背景图失败");
						map.put("state", 0);
					}
				}
				return map;
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 1);
				return map;
			}
		}
	}


	@RequestMapping(value = "update_nickname")//用户修改昵称
	@ResponseBody
	public  Map<String, Object> UpdateNickname(Integer user_id,UgcUser u,String nickname,
			HttpServletResponse response,String secret,String pust_val){
		response.setHeader("access-control-allow-origin","*");
		Map<String,Object> map=new HashMap<String, Object>();
		if(pust_val==null||secret==null||user_id==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
			String mds=Md.MdSecret(pust_val+"flower_duigu2016");
			if(secret.endsWith(mds)){
				u.setId(user_id);
				List<Object> userinf=new ArrayList<Object>();
				userinf=userServic.getByuid(u);
				for (Object object : userinf) {
					UgcUser user=(UgcUser) object;
					user.setNickname(nickname);
					boolean b=userServic.updateU(user);
					if(b){
						map.put("message", "修改昵称成功");
						map.put("state", 1);
					}else{
						map.put("message", "修改昵称失败");
						map.put("state", 0);
					}
				}
				return map;
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 1);
				return map;
			}
		}
	}

	@RequestMapping(value = "update_sex")//用户修改性别
	@ResponseBody
	public  Map<String, Object> UpdateSex(Integer user_id,UgcUser u,String sex,
			HttpServletResponse response,String secret,String pust_val){
		response.setHeader("access-control-allow-origin","*");
		Map<String,Object> map=new HashMap<String, Object>();
		if(pust_val==null||secret==null||user_id==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
			String mds=Md.MdSecret(pust_val+"flower_duigu2016");
			if(secret.endsWith(mds)){
				u.setId(user_id);
				List<Object> userinf=new ArrayList<Object>();
				userinf=userServic.getByuid(u);
				for (Object object : userinf) {
					UgcUser user=(UgcUser) object;
					user.setSex(sex);
					boolean b=userServic.updateU(user);
					if(b){
						map.put("message", "修改性别成功");
						map.put("state", 1);
					}else{
						map.put("message", "修改性别失败");
						map.put("state", 0);
					}
				}
				return map;
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 1);
				return map;
			}
		}
	}

	@RequestMapping(value = "update_place")//用户修改居住地
	@ResponseBody
	public  Map<String, Object> UpdatePlace(Integer user_id,UgcUser u,String city,String province,
			HttpServletResponse response,String secret,String pust_val){
		response.setHeader("access-control-allow-origin","*");
		Map<String,Object> map=new HashMap<String, Object>();
		if(pust_val==null||secret==null||user_id==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
			String mds=Md.MdSecret(pust_val+"flower_duigu2016");
			if(secret.endsWith(mds)){
				u.setId(user_id);
				List<Object> userinf=new ArrayList<Object>();
				userinf=userServic.getByuid(u);
				for (Object object : userinf) {
					UgcUser user=(UgcUser) object;
					user.setCity(city);
					user.setProvince(province);
					boolean b=userServic.updateU(user);
					if(b){
						map.put("message", "修改居住地成功");
						map.put("state", 1);
					}else{
						map.put("message", "修改居住地失败");
						map.put("state", 0);
					}
				}
				return map;
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 1);
				return map;
			}
		}
	}


	@RequestMapping(value = "update_signature")//用户修改个性签名
	@ResponseBody
	public  Map<String, Object> UpdateSignature(Integer user_id,UgcUser u,String signature,HttpServletResponse response,
			String secret,String pust_val){
		response.setHeader("access-control-allow-origin","*");
		Map<String,Object> map=new HashMap<String, Object>();
		if(pust_val==null||secret==null||user_id==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
			String mds=Md.MdSecret(pust_val+"flower_duigu2016");
			if(secret.endsWith(mds)){
				u.setId(user_id);
				List<Object> userinf=new ArrayList<Object>();
				userinf=userServic.getByuid(u);
				for (Object object : userinf) {
					UgcUser user=(UgcUser) object;
					user.setSignature(signature);
					boolean b=userServic.updateU(user);
					if(b){
						map.put("message", "修改个性签名成功");
						map.put("state", 1);
					}else{
						map.put("message", "修改个性签名失败");
						map.put("state", 0);
					}
				}
				return map;
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 1);
				return map;
			}
		}
	}




}
